<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        border
        :where="defaultWhere"
      >
        <template #status="{ row }">
          <el-tag :type="row.statusType">
            {{ row.statusText }}
          </el-tag>
        </template>
        <template #plan_date="{ row }">
          {{ row.plan_date }}
          <br />
          <span class="red">{{ row.day }}</span>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="success" size="small" @click="openInfo(row)">
              填写退保信息
            </el-button>
            <el-button type="danger" size="small" @click="abort(row)">
              取消计划
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
      <!-- 退保信息填写 -->
      <info v-model="showInfo" :data="current" @done="reload" />
    </ele-card>
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import {
    pageInsuranceRefundPlan,
    delInsuranceRefundPlan
  } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  import Search from './components/search.vue';
  import Info from './components/info.vue';

  /** 表格实例 */
  const tableRef = ref(null);
  const defaultWhere = {
    status: 0,
    deadline: '未过期'
  };

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: 'ID'
      },
      {
        width: 100,
        align: 'center',
        slot: 'status',
        label: '状态'
      },
      {
        width: 100,
        align: 'center',
        prop: 'create_user',
        label: '创建人'
      },
      {
        width: 140,
        align: 'center',
        prop: 'create_time',
        label: '创建时间'
      },
      {
        width: 130,
        align: 'center',
        slot: 'plan_date',
        label: '计划退保日期'
      },
      {
        prop: 'remark',
        label: '计划内容'
      },
      {
        width: 120,
        prop: 'plate_number',
        label: '相关车牌'
      },
      {
        width: 120,
        prop: 'cate',
        label: '保险种类'
      },
      {
        width: 180,
        prop: 'insurance',
        label: '保险公司'
      },
      {
        width: 100,
        prop: 'insurance_salesman',
        label: '业务员'
      }
    );
    if (status.value == 0) {
      cols.push({
        slot: 'action',
        label: '操作'
      });
    }

    return cols;
  });

  const abort = (row) => {
    ElMessageBox.confirm(
      '确定要取消' + row.plate_number + '的退保计划吗？',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        delInsuranceRefundPlan({ id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsuranceRefundPlan({
      ...where,
      ...orders,
      page,
      limit
    });
    return result;
  };
  const status = ref(0);
  /** 搜索 */
  const reload = (where) => {
    status.value = where ? where.status : 0;
    tableRef.value?.reload?.({ page: 1, where });
  };

  const current = ref([]);

  /** 填写退保信息 */
  const showInfo = ref(false);
  const openInfo = (row) => {
    current.value = row;
    showInfo.value = true;
  };
</script>
